package com.xls.mypay;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.xls.mypay.anim.Rotate3dAnimation;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private static final String TAG ="MainActivity";
    private RelativeLayout containerRl;
    private Button awardBtn;
    private Button thankbtn;
    private int centerX;
    private int centerY;
    private int depthZ = 100;
    private int duration = 600;
    private Rotate3dAnimation openAnimation;
    private Rotate3dAnimation closeAnimation;
    boolean isOpen =false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        containerRl = (RelativeLayout) findViewById(R.id.rl_container);
        awardBtn = (Button) findViewById(R.id.btn_award);
        thankbtn = (Button) findViewById(R.id.btn_thanks);
        awardBtn.setOnClickListener(this);
        thankbtn.setOnClickListener(this);


    }






    /**
     * 卡牌文本介绍打开效果：注意旋转角度
     */
    private void initOpenAnim() {
        //从0到90度，顺时针旋转视图，此时reverse参数为true，达到90度时动画结束时视图变得不可见，
        openAnimation = new Rotate3dAnimation(0, 90, centerX, centerY, depthZ, true);
        openAnimation.setDuration(duration);
        openAnimation.setFillAfter(true);
        openAnimation.setInterpolator(new AccelerateInterpolator());
        openAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.d(TAG,"open--onAnimationStart");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.d(TAG,"open--onAnimationRepeat");

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.d(TAG,"open--onAnimationEnd");
                thankbtn.setVisibility(View.VISIBLE);
                awardBtn.setVisibility(View.GONE);

                //从270到360度，顺时针旋转视图，此时reverse参数为false，达到360度动画结束时视图变得可见
                Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(270, 360, centerX, centerY, depthZ, false);
                rotateAnimation.setDuration(duration);
                rotateAnimation.setFillAfter(true);
                rotateAnimation.setInterpolator(new DecelerateInterpolator());
                containerRl.startAnimation(rotateAnimation);
            }
        });


    }

    /**
     * 卡牌文本介绍关闭效果：旋转角度与打开时逆行即可
     */
    private void initCloseAnim() {
        closeAnimation = new Rotate3dAnimation(360, 270, centerX, centerY, depthZ, true);
        closeAnimation.setDuration(duration);
        closeAnimation.setFillAfter(true);
        closeAnimation.setInterpolator(new AccelerateInterpolator());
        closeAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                Log.d(TAG,"close--onAnimationStart");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.d(TAG,"close--onAnimationRepeat");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                thankbtn.setVisibility(View.GONE);
                awardBtn.setVisibility(View.VISIBLE);

                Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(90, 0, centerX, centerY, depthZ, false);
                rotateAnimation.setDuration(duration);
                rotateAnimation.setFillAfter(true);
                rotateAnimation.setInterpolator(new DecelerateInterpolator());
                containerRl.startAnimation(rotateAnimation);
            }
        });
    }


    @Override
    public void onClick(View v) {

        centerX = containerRl.getWidth()/2;
        centerY = containerRl.getHeight()/2;

        Log.d(TAG,"centerX = "+centerX+" |  centerY = "+centerY);

        if (openAnimation == null) {
            initOpenAnim();
            initCloseAnim();
        }
        //用作判断当前点击事件发生时动画是否正在执行
        if (openAnimation.hasStarted() && !openAnimation.hasEnded()) {
            return;
        }
        if (closeAnimation.hasStarted() && !closeAnimation.hasEnded()) {
            return;
        }


        switch (v.getId()){
            case R.id.btn_award:
                containerRl.startAnimation(openAnimation);
                break;
            case R.id.btn_thanks:
                containerRl.startAnimation(closeAnimation);
                break;
        }
    }
}
